package com.xingda.packing.dmss.minitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.dmss.domain.PackProductionLine;
import com.xingda.packing.dmss.enums.TaskLocation;
import com.xingda.packing.dmss.enums.WheelHeight;
import com.xingda.packing.dmss.minitor.entity.PkBoxLocation;
import com.xingda.packing.dmss.minitor.mapper.PkBoxLocationMapper;
import com.xingda.packing.dmss.minitor.service.MachineStatusService;
import com.xingda.packing.dmss.plc.service.PlcDirectiveService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MachineStatusServiceImpl implements MachineStatusService {
    @Resource
    private PlcDirectiveService plcDirectiveService;
    @Resource
    private PkBoxLocationMapper pkBoxLocationMapper;
    @Resource
    private PackProductionLine packProductionLine;
    @Value("${spring.version}")
    @Getter
    private String version;

    @Override
    public List<String> status(int area) {
        //0中 1左 2右
        List<String> list = new ArrayList<>();
        if (area != 0) {
            list.add("机器人-" + area + "MES 版本号:" + version);
            list.add("机器人-" + area + "-可以下发任务:" + (plcDirectiveService.canAssignTask(area) ? "可以" : "不可以"));
            list.add("机器人-" + area + "-缓存位-装箱位是否工作:" + (packProductionLine.getRobot().getWorkStatus(area) ? "工作" : "停止"));
            list.add("机器人-" + area + "-抓取完成:" + (plcDirectiveService.isGrabComplete(area) ? "是" : "否"));
            list.add("机器人-" + area + "-码垛完成:" + (plcDirectiveService.isPalletizingComplete(area) ? "是" : "否"));
            list.add("机器人-" + area + "-条形码读取触发:" + (plcDirectiveService.isScanSignal(area) ? "是" : "否"));
            list.add("机器人-" + area + "-条码:" + plcDirectiveService.getScanWheelNo(area));
            list.add("机器人-" + area + "-抓取位有轮子:" + (plcDirectiveService.isHaveWheel(area) ? "是" : "否"));
            list.add("机器人-" + area + "-机器状态:" + (plcDirectiveService.getRobotStatus(area) ? "关闭" : "开启"));
            list.add("机器人-" + area + "-高低轮:" + WheelHeight.getByID(plcDirectiveService.getProductTypeToZeroGrab(area)).getTypeName());
            list.add("机器人-" + area + "-产品类别:" + WheelHeight.getByID(plcDirectiveService.getProductType(area)).getTypeName());
            list.add("机器人-" + area + "-MES下发任务完成:" + (plcDirectiveService.getMesTask(area) ? "是" : "否"));
            list.add("机器人-" + area + "-任务位置:" + TaskLocation.getByID(plcDirectiveService.getTaskType(area)).getTypeName());
            list.add("缓存位高轮最大位置号:" + packProductionLine.getCacheArea().getMaxB80Num(area));
            list.add("缓存位低轮最大位置号:" + packProductionLine.getCacheArea().getMaxB60Num(area));
            list.add("缓存位下个高轮放位置号:" + packProductionLine.getCacheArea().nextPutLocationNo(2, area).get().getNo());
            list.add("缓存位下个低轮放位置号:" + packProductionLine.getCacheArea().nextPutLocationNo(1, area).get().getNo());
            list.add("=============");
            LambdaQueryWrapper<PkBoxLocation> pkBoxLocationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (area == 1) {
                pkBoxLocationLambdaQueryWrapper.between(PkBoxLocation::getId, 1, 6);
            } else {
                pkBoxLocationLambdaQueryWrapper.between(PkBoxLocation::getId, 7, 12);
            }
            List<PkBoxLocation> pkBoxLocations = pkBoxLocationMapper.selectList(pkBoxLocationLambdaQueryWrapper);
            for (PkBoxLocation pkBoxLocation : pkBoxLocations) {
                list.add(pkBoxLocation.getBoxLocationName() + "拍照完成:" + (plcDirectiveService.isPhotoFinish(pkBoxLocation.getPalletPhotoFinish()) ? "是" : "否"));
                list.add(pkBoxLocation.getBoxLocationName() + "托盘放置:" + (plcDirectiveService.isTrue(pkBoxLocation.getPalletComing()) ? "是" : "否"));
                list.add(pkBoxLocation.getBoxLocationName() + "托盘拿走:" + (plcDirectiveService.isTrue(pkBoxLocation.getPalletMove()) ? "是" : "否"));
                list.add(pkBoxLocation.getBoxLocationName() + "码垛数量:" + (plcDirectiveService.getPalletPackingCount(pkBoxLocation.getPalletPackingCount())));
                list.add(pkBoxLocation.getBoxLocationName() + "托盘类型:" + (WheelHeight.getByID(plcDirectiveService.getPalletWheelNumSet(pkBoxLocation.getPalletWheelNumSet()))).getTypeName());
                list.add("=============");
            }
        } else {
            list.add("分流区-触发MES解析条码:" + (plcDirectiveService.isShuntAreaSignal() ? "是" : "否"));
            list.add("分流区-MES解析条码完成:" + (plcDirectiveService.isCodeParsingSucceeded() ? "是" : "否"));
            list.add("分流区-MES解析条码错误:" + (plcDirectiveService.isCodeParsingFailed() ? "是" : "否"));
            list.add("=============");
            list.add("分流区-触发MES解析卡扣:" + (plcDirectiveService.isSnapSignal() ? "是" : "否"));
            list.add("分流区-MES解析卡扣完成:" + (plcDirectiveService.isSnapParsingSucceeded() ? "是" : "否"));
            list.add("分流区-MES解析卡扣错误:" + (plcDirectiveService.isSnapParsingFailed() ? "是" : "否"));
            list.add("=============");
            list.add("分流区-条形码:" + plcDirectiveService.getShuntAreaScanWheelNo());
            list.add("分流区-分流完成:" + (plcDirectiveService.isShuntOver() ? "是" : "否"));
            list.add("分流区-高低轮:" + WheelHeight.getByID(plcDirectiveService.getProductTypeToShunt()).getTypeName());
            list.add("分流区-心跳:" + (plcDirectiveService.getPlcStatus() ? "异常" : "正常"));
            list.add("=============");
            list.add("AGV满轮托盘号:" + plcDirectiveService.agvTPH1());
            list.add("AGV请求取料:" + plcDirectiveService.isPackingStatus());
            list.add("内收线允许取料:" + plcDirectiveService.allowTask1());
            list.add("AGV在取料区域:" + plcDirectiveService.isPacking());
            list.add("AGV取料完成:" + plcDirectiveService.isPackingOk());
            list.add("=============");
            list.add("AGV请求放托盘号:" + plcDirectiveService.agvTPH2());
            list.add("AGV请求放托盘:" + plcDirectiveService.isKongTuoStatus());
            list.add("内收线允许放托盘:" + plcDirectiveService.allowTask2());
            list.add("AGV在放托盘区域:" + plcDirectiveService.isKongTuo());
            list.add("AGV放托盘完成:" + plcDirectiveService.isKongTuoOk());
            list.add("=============");
            list.add("内收线紧急停止:" + plcDirectiveService.isStopSignal());
            list.add("复位AGV:" + plcDirectiveService.isReset());
            list.add("AGV到位:" + packProductionLine.isAgvComing());
        }
        return list;
    }
}
